var toolBar = (function () {

    var ex = {};

    ex.init = function () {
        var $toolbar = $('<div id="crx-toolbar"></div>');
        $toolbar.append('<a id="crx-app-link" href="' + chrome.runtime.getURL('app.html') + '" target="_blank">管理</a>');
        $toolbar.append('<a id="crx-write-link" href="' + chrome.runtime.getURL('editor.html') + '" target="_blank">撰写</a>');
        $('body').css('paddingTop', '31px').prepend($toolbar);
    };

    return ex;

}());

var blogScript = (function () {

    var ex = {};

    function _addLink2List() {
        var $list = $('#blog-list');
        $list.find('.blog-list-item').each(function (i, item) {
            var blogId = item.id.replace('blog-', '');
            var str = '<a target="_blank" href="' + chrome.runtime.getURL('editor.html?id=' + blogId) + '" class="crx-blog-edit" rel="' + blogId + '">编辑</a>'
                + '<a href="#" class="crx-blog-hide" rel="' + blogId + '">隐藏</a>';
            $(item).find('.blog-title').append(str);
        });
    }

    function _addLink2One() {
        var $item = $('.blog-item');
        if (!$item[0]) {
            return;
        }
        var blogId = $item[0].id.replace('blog-', '');
        var str = '<a target="_blank" href="' + chrome.runtime.getURL('editor.html?id=' + blogId) + '" class="crx-blog-edit" rel="' + blogId + '">编辑</a>'
            + '<a href="#" class="crx-blog-hide" rel="' + blogId + '">隐藏</a>';
        $item.find('.blog-title h2').after(str);
    }

    function _initHideEvent() {
        var str = '<div id="crx-blog-hide-overlay"><strong></strong>' +
            '<input class="button" value="隐藏" id="crx-blog-hide-submit" type="button">' +
            '<input class="button close" type="button" value="算了"/>' +
            '</div>';
        $('body').append(str);
        $('.crx-blog-hide').overlay({
            target: "#crx-blog-hide-overlay",
            mask: {
                color: '#F1F1FF',
                opacity: 0.9,
                loadSpeed: 'fast'
            },
            speed: 'false',
            closeOnClick: false,
            onBeforeLoad: function (e) {
                var title = $(e.target).parents('.blog-title').find('a:first-child').text();
                $('#crx-blog-hide-overlay strong').text(title);
                $('#crx-blog-hide-submit').data('id', $(e.target).attr('rel')).show();
            }
        });
        $('#crx-blog-hide-submit').on('click', function () {
            $('#crx-blog-hide-overlay').append('<p class="result">处理中</p>');
            chrome.runtime.sendMessage({
                event: 'content.blog.change',
                data: {
                    id: $('#crx-blog-hide-submit').data('id'),
                    status: 'draft'
                }
            })
        });
    }

    ex.init = function () {
        _addLink2List();
        _addLink2One();
        _initHideEvent();
    };

    ex.resultChangeStatus = function (status) {
        if (status == 'draft') {
            $('#crx-blog-hide-overlay .result').text('文章已隐藏，刷新后不可见');
            $('#crx-blog-hide-submit').hide();
        }
    };

    return ex;

}());

$(document).ready(function () {

    chrome.runtime.onMessage.addListener(function (message) {
        if (message.event == 'content.blog.change.result') {
            blogScript.resultChangeStatus(message.data.change);
        }
    });

    toolBar.init();
    blogScript.init();
});